<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品ID" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家ID" prop="businessId">
        <el-input
          v-model="queryParams.businessId"
          placeholder="请输入商家ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品规格信息" prop="goodsSpecs">
        <el-input
          v-model="queryParams.goodsSpecs"
          placeholder="请输入商品规格信息"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付价格" prop="orderPrice">
        <el-input
          v-model="queryParams.orderPrice"
          placeholder="请输入支付价格"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单创建时间" prop="orderCreateTime">
        <el-date-picker clearable
          v-model="queryParams.orderCreateTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择订单创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单付款时间" prop="orderPayTime">
        <el-date-picker clearable
          v-model="queryParams.orderPayTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择订单付款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单发货时间" prop="orderPostTime">
        <el-date-picker clearable
          v-model="queryParams.orderPostTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择订单发货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单成交时间" prop="orderSuccessTime">
        <el-date-picker clearable
          v-model="queryParams.orderSuccessTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择订单成交时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="运费" prop="orderPostPrice">
        <el-input
          v-model="queryParams.orderPostPrice"
          placeholder="请输入运费"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单备注" prop="orderComment">
        <el-input
          v-model="queryParams.orderComment"
          placeholder="请输入订单备注"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单物流单号" prop="orderTrackNumber">
        <el-input
          v-model="queryParams.orderTrackNumber"
          placeholder="请输入订单物流单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址ID" prop="addressId">
        <el-input
          v-model="queryParams.addressId"
          placeholder="请输入地址ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['zmhshop:orderst:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zmhshop:orderst:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zmhshop:orderst:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['zmhshop:orderst:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderstList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="买家" align="center" prop="username" />
      <el-table-column label="支付价格" align="center" prop="orderPrice" />
      <el-table-column label="订单创建时间" align="center" prop="orderCreateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单付款时间" align="center" prop="orderPayTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderPayTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单发货时间" align="center" prop="orderPostTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderPostTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单成交时间" align="center" prop="orderSuccessTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderSuccessTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <el-tag :type="getStatusTagType(scope.row.orderStatus)">
            {{ getStatusText(scope.row.orderStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['zmhshop:orderst:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['zmhshop:orderst:remove']">删除</el-button>
          <el-button  type="danger"  @click="handleFind(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderstRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="商品ID" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品ID" />
        </el-form-item>
        <el-form-item label="商家ID" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商家ID" />
        </el-form-item>
        <el-form-item label="商品规格信息" prop="goodsSpecs">
          <el-input v-model="form.goodsSpecs" placeholder="请输入商品规格信息" />
        </el-form-item>
        <el-form-item label="支付价格" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入支付价格" />
        </el-form-item>
        <el-form-item label="订单创建时间" prop="orderCreateTime">
          <el-date-picker clearable
            v-model="form.orderCreateTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择订单创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单付款时间" prop="orderPayTime">
          <el-date-picker clearable
            v-model="form.orderPayTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择订单付款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单发货时间" prop="orderPostTime">
          <el-date-picker clearable
            v-model="form.orderPostTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择订单发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单成交时间" prop="orderSuccessTime">
          <el-date-picker clearable
            v-model="form.orderSuccessTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择订单成交时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="运费" prop="orderPostPrice">
          <el-input v-model="form.orderPostPrice" placeholder="请输入运费" />
        </el-form-item>
        <el-form-item label="订单备注" prop="orderComment">
          <el-input v-model="form.orderComment" placeholder="请输入订单备注" />
        </el-form-item>
        <el-form-item label="订单单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单单号" />
        </el-form-item>
        <el-form-item label="订单物流单号" prop="orderTrackNumber">
          <el-input v-model="form.orderTrackNumber" placeholder="请输入订单物流单号" />
        </el-form-item>
        <el-form-item label="地址ID" prop="addressId">
          <el-input v-model="form.addressId" placeholder="请输入地址ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Orderst">
import { listOrderst, getOrderst, delOrderst, addOrderst, updateOrderst } from "@/api/zmhshop/orderst"
const { proxy } = getCurrentInstance()
const orderstList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    goodsId: null,
    businessId: null,
    goodsSpecs: null,
    orderPrice: null,
    orderCreateTime: null,
    orderPayTime: null,
    orderPostTime: null,
    orderSuccessTime: null,
    orderPostPrice: null,
    orderStatus: null,
    orderComment: null,
    orderNumber: null,
    orderTrackNumber: null,
    orderType: null,
    addressId: null,
    username: null,
    goodsVirtual: 0 || 1,
  },
  rules: {
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    goodsId: [
      { required: true, message: "商品ID不能为空", trigger: "blur" }
    ],
    businessId: [
      { required: true, message: "商家ID不能为空", trigger: "blur" }
    ],
    orderPrice: [
      { required: true, message: "支付价格不能为空", trigger: "blur" }
    ],
    orderCreateTime: [
      { required: true, message: "订单创建时间不能为空", trigger: "blur" }
    ],
    orderStatus: [
      { required: true, message: "订单状态(0-待支付,1-已支付,2-已发货,3-已完成,4-已取消)不能为空", trigger: "change" }
    ],
    orderNumber: [
      { required: true, message: "订单单号不能为空", trigger: "blur" }
    ],
    addressId: [
      { required: true, message: "地址ID不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询订单列表 */
function getList() {
  loading.value = true
  listOrderst().then(response => {
    orderstList.value = response.rows
    console.log(888888)
    console.log(orderstList)
    orderstList.value = response.rows.filter(item => item.goodsVirtual === 1)
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    userId: null,
    goodsId: null,
    businessId: null,
    goodsSpecs: null,
    orderPrice: null,
    orderCreateTime: null,
    orderPayTime: null,
    orderPostTime: null,
    orderSuccessTime: null,
    orderPostPrice: null,
    orderStatus: null,
    orderComment: null,
    orderNumber: null,
    orderTrackNumber: null,
    orderType: null,
    addressId: null,

  }
  proxy.resetForm("orderstRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加订单"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _orderId = row.orderId || ids.value
  getOrderst(_orderId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改订单"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderstRef"].validate(valid => {
    if (valid) {
      if (form.value.orderId != null) {
        updateOrderst(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addOrderst(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderIds = row.orderId || ids.value
  proxy.$modal.confirm('是否确认删除订单编号为"' + _orderIds + '"的数据项？').then(function() {
    return delOrderst(_orderIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}
function handleFind(row) {
  const orderId = row.orderId // 获取当前行的订单ID
  // 使用路由跳转到订单详情页，并传递 orderId 参数
  proxy.$router.push({
    path: '/orderst/detail', // 替换成你的订单详情页路由
    // query: { orderId }    // 传递 orderId 参数
    query: {
      orderId: orderId,
    }
  })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('zmhshop/orderst/export', {
    ...queryParams.value
  }, `orderst_${new Date().getTime()}.xlsx`)
}

// 订单状态文本映射
function getStatusText(status) {
  const statusMap = {
    0: '待支付',
    1: '已支付',
    2: '已发货',
    3: '已完成',
    4: '已取消'
  }
  return statusMap[status] || '未知状态'
}

// 订单状态标签类型
function getStatusTagType(status) {
  const typeMap = {
    0: 'warning',
    1: 'primary',
    2: 'info',
    3: 'success',
    4: 'danger'
  }
  return typeMap[status] || ''
}

getList()
</script>
